$().ready(function() {
	validateRule();
	initData();
	initBtn();
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
    $("#parentId").removeAttr("disabled");
	$.ajax({
		cache : true,
		type : "POST",
		url : "/sys/menu/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入菜单名称"
			}
		}
	})
}
function initData() {
    $.ajax({
        url: '/sys/menu/selectRoleMenus',
        type: 'post',
        data: {},
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data) {
                var d = JSON.parse(data);
                var oldParentId = $(":hidden[name=oldParentId]").val();
                $(d).each(function () {
                	if(oldParentId == this.menuId){
                        $("#parentId").append("<option value='" + this.menuId + "' selected='selected'>" + this.name + "</option>");
					}else{
                        $("#parentId").append("<option value='" + this.menuId + "'>" + this.name + "</option>");
					}

                });
            }
        }
    });

}
function initBtn(){
	var oldType = $(":hidden[name=oldType]").val();
    document.getElementById('type').value=oldType;
    $("#type").change(function(){
        if(0 == this.value){
            document.getElementById('parentId').value=this.value;
            $("#parentId").attr("disabled","disabled");
        }else{
            $("#parentId").removeAttr("disabled");
        }
    });
}